package com.aube.mis.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aube.mis.dto.ActorDTO;
import com.aube.mis.dto.PaginationResultDTO;
import com.aube.mis.dto.ShowDTO;
import com.aube.mis.dto.ShowLiveSettingDTO;
import com.aube.mis.dto.TemplateDTO;
import com.aube.mis.dto.VideoDTO;
import com.aube.mis.service.ShowService;
import com.aube.mis.util.Constant;
import com.aube.mis.util.JsonUtil;
import com.aube.mis.vo.DataTableCriterias;

@Controller
@RequestMapping(value = "/show")
public class ShowController extends AbstractController {

	private static Logger log = LoggerFactory.getLogger(ShowController.class);

	@Resource
	ShowService showService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(HttpServletResponse response, ModelMap map) {
		return "/show/list";
	}

	@RequestMapping(value = "/list/data.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String listData(ModelMap map, @ModelAttribute DataTableCriterias criterias) {
		PaginationResultDTO<ShowDTO> result = showService.getShowList(criterias);
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("list", result.getResultList());
		dataMap.put("total", result.getTotal());
		return this.createSuccessResponse(dataMap);
	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(HttpServletResponse response, ModelMap map, String showId) {
		if (StringUtils.isNotEmpty(showId)) {
			ShowDTO show = showService.getShowDetail(showId);
			map.put("show", show);
		}
		return "/show/edit.page";
	}

	@RequestMapping(value = "/edit.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String editAjax(HttpServletResponse response, @ModelAttribute ShowDTO showDTO) {
		showService.editShow(showDTO);
		reloadShowList();
		return this.createSuccessResponse();
	}

	@RequestMapping(value = "/delete.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String videoDelete(String showId) {
		showService.deleteShow(showId);
		reloadShowList();
		return this.createSuccessResponse();
	}

	private void reloadShowList() {
		PaginationResultDTO<ShowDTO> showList = showService.getShowList(new DataTableCriterias(0, 9999));
		getRequest().getSession().setAttribute(Constant.Attr.SHOW_LIST, showList.getResultList());
	}
}
